package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    //在支付宝创建的应用的id
    private String app_id = "2016102600765903";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCvLtMIDaz/EaWopS1KTnI6c/hfbxjHGeql0NH7L2YPyHvVgNho5esguAN5Bbq+LBdpo8J43gpLwezmNa7G3BQXLJK6MvkpasDaS4WAWJ9KWvGHYdThV5JfiICrgugweNoH/eXE4q0ZgrFRKa/GP1QKazfN2Aku4sy3FogmIUKTTs5oKoPDlj9Vf8klVWHlMtl/rosjmijGL6TseG2EoxP4tcN78oGt6J4L7I4jkKDrV8r7bi0ndfaVLJdEo/ZC6w5U6FBAp2MCoRydXkP4E/zxAlTSxnS4RKROmF95fyD41qtRb30nEQHw/N/PSVf2IKYmHiOHE1QvKaG4xMROI/SXAgMBAAECggEAUaSB51vwRwOjb+BjJiE/LSzFET0GkCvs5pBWfDkP2MU3qT4eHW0h0qLPw4Q/DsgftTUDui7JH5tmW7ejF7yThGVakHqXEo1VCadutW0X96BAtjaK7uZQKkPEq6obqhN4kYbroYF+6Me2+bty1vLGqQBlBoLrbLV4cM+IbjTMAThP10PRbUcs7G3E11FkuA2TAJb8Csa5X2DXAkWT0Ye0Vamx9CUpYxlanDiC++9qogFWySDmIEjdG8PQoAyU50ILgiQwqybIxV4FnDn46M91C8qYbeCl42ctNFlKDa8unXOeEejeZPMlW5IdR+KTBi6J0CmCqOZlQKNPtFl1xo/iAQKBgQDn5cSxHXRw2tv/3EDEPtpNdfCu2J+362s254/oIS4J0TQhH2L0xrq1euN1YVNJODtUxKUDmBnF1CkiZqes2NWI55WNyqJ4CSwH8MYZrqVeQYH61JWtAg6ZUQSiLSn9Rx4RhruqsdMNmfAT10Jw8U4b9DCMZmWyV3i768QanP+ZlwKBgQDBZARn4gwYsXu8kd98AJAxMlV+34+zF/wjoZ1YcgDYqGHLry+TigLnn3wD9c7zqVJZWAE7uLCj8h0cAZsNS1ldMZTiRDQS0+rzpgSweFlw0bfaUe5D+AxY/nHVGAVYBhauHOKBVXAGEc4v779D8q4L2DM3zvCvFdG752UbcnvdAQKBgFaqo5kCBhuLX/8+pKHzT4j1lA70fLSy8i3h2wGJLPiUTCQ447XRc8QRemLLDeJmdp4WpAk01iT+RqVJsk7B9wQkcNs+KPwtiVwqWw8MG1efqeFBg/LVuvkHs3Cc6n1zD8TDEu4XaWZC6tuvbnBhbqy51U8TkdxWHpQ05+R+WnRfAoGBAL6z1Cxkmi0Xh78x0cHEBGyfB/PunROsSTURv8lh0jfUA2wJus6wv6CMgoydLpDUnaIAyGaN/5ILxGzDoJSBgwQFSp29R+RxGTVety0V4I0CIVymgmCAEB0J/FJ4N3zPtDnJvgpvp+I0WLCoqGFmw/5mGkukQ29EAuhajDd+s2oBAoGAHMFA+mcJMbxR9VbYvt9qGvwHTxxHS/V710yjuj3yciMryA9cg2SC4wChWOeAWVtvr7XRP0DGqmy0AoDuiB0kaUOQrv+PUwJXzCuzD2lPnCd7919dKJ6Ugnn+OdRtMt3daUBrZ9JeIKe7yMfPlkQfvmppCi2AzedLxjE3gVW6110=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv7P6fVNtkoyo1GbDHl8juHmgIsVFKl0lJG1L74qIvQs3adnGPz/N+1s/qEn90zNpdp+OarEHnHCfTf6l3fXI7b50o8Mz4HcshmNOQOzO67ApDGlnVwzJjx9SDabd4uIcRU9t/FN0woX6HAWoNZBUIQSHNDKP0vccK63IUjH7AFFbstiOWwVRcS53Vvw2ozNF4jM8vQeBIup6Z9Er56tqcXa78rk1NJ/ac8913bSHXCe4d68uJ+a+KcT/oSTe4iGnv0RqL0QGCIrej9OF0OFAEzfXV7R/lQJ27snrqR00srDxNLCbZdY8kfHGf5l8pIpNQrO/fATgmL8CtYYYhgGzSwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://dtcaksomqx.xuduan.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;
    }
}
